﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yunchang.ILUI
{
    public class UIExtendCtrl
    {
        private UIExtendPart[] curParts;
        private UIExtendPart[] allParts;
        private object[] defaultArgs;

        private Dictionary<int, object[]> allPartArgs = new Dictionary<int, object[]>();

        /// <summary>
        /// 生成控制器
        /// </summary>
        /// <param name="partSize"></param>
        /// <param name="layoutSize"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static UIExtendCtrl New(int partSize, int layoutSize, object[] args = null)
        {
            UIExtendCtrl ctrl = new UIExtendCtrl();
            ctrl.curParts = new UIExtendPart[layoutSize];
            ctrl.allParts = new UIExtendPart[partSize];
            ctrl.defaultArgs = args;
            return ctrl;
        }

        public void ResetArgs(int partIndex, object[] args)
        {
            if (allPartArgs.ContainsKey(partIndex))
                allPartArgs[partIndex] = args;
            else
                allPartArgs.Add(partIndex, args);
        }

        /// <summary>
        /// 重置参数
        /// </summary>
        /// <param name="args"></param>
        public void ResetDefaultArgs(object[] args)
        {
            defaultArgs = args;
        }

        public object[] GetArgs(int partIndex)
        {
            if (allPartArgs.ContainsKey(partIndex))
                return allPartArgs[partIndex];
            else
                return defaultArgs;
        }
        /// <summary>
        /// 获取当前附加部件
        /// </summary>
        /// <param name="layout"></param>
        /// <returns></returns>
        public UIExtendPart GetCurExtendPart(int layout)
        {
            if (curParts != null && layout < curParts.Length)
            {
                return curParts[layout];
            }
            return null;
        }

        /// <summary>
        /// 初始化当前所有附加部件
        /// </summary>
        /// <returns></returns>
        public bool InitCurExtendParts()
        {
            if (curParts == null || curParts.Length < 1) return false;

            bool hasPart = false;
            for (int i = 0, len = curParts.Length; i < len; i++)
            {
                var part = curParts[i];
                if (part != null)
                {
                    var args = GetArgs(part.partIndex);
                    part.InitPart(args);
                    hasPart = true;
                }
            }
            return hasPart;
        }

        /// <summary>
        /// 重置当前所有附加部件
        /// </summary>
        /// <returns></returns>
        public bool ResetCurExtendParts()
        {
            if (curParts == null || curParts.Length < 1) return false;

            bool hasPart = false;
            for (int i = 0 , len = curParts.Length; i < len; i++)
            {
                if (curParts[i] != null)
                {
                    curParts[i].ResetPart();
                    hasPart = true;
                }
            }
            return hasPart;
        }
        /// <summary>
        /// 通过部件索引获取附加部件
        /// </summary>
        /// <param name="partIndex"></param>
        /// <returns></returns>
        public UIExtendPart GetExtendPart(int partIndex)
        {
            if (allPartArgs != null && partIndex < allParts.Length)
            {
                return allParts[partIndex];
            }
            return null;
        }

        /// <summary>
        /// 设置附加部件
        /// </summary>
        /// <param name="part"></param>
        public void SetExtendPart(UIExtendPart part)
        {
            if (part == null) return;

            if (allParts == null || part.partIndex >= allParts.Length)
            {
                GLog.Error("SetExtendPartError: Part index is out of range.");
                return;
            }
            allParts[part.partIndex] = part;
        }

        /// <summary>
        /// 显示附加部件
        /// </summary>
        /// <param name="part"></param>
        public void ShowExtendPart(UIExtendPart part)
        {
            int idx = (int)part.layout;
            //if (curParts[idx] == part) return;

            if (curParts[idx] != null)
            {
                curParts[idx].SetActive(false);
                curParts[idx].ResetPart();
            }

            curParts[idx] = part;

            part.SetActive(true);
            var args = GetArgs(part.partIndex);
            part.InitPart(args);
        }

        /// <summary>
        /// 隐藏附加部件
        /// </summary>
        /// <param name="layout"></param>
        public void HideExtendPart(UIPartLayout layout)
        {
            int idx = (int)layout;

            if (curParts[idx] != null)
            {
                curParts[idx].SetActive(false);
                curParts[idx].ResetPart();
                curParts[idx] = null;
            }
        }

        /// <summary>
        /// 清空控制器内组件
        /// </summary>
        public void Clear()
        {
            if (allParts != null)
            {
                for (int i = 0, len = allParts.Length; i < len; i++)
                {
                    if (allParts[i] != null) allParts[i].ResetPart();
                }
                allParts = null;
            }

            if (allPartArgs != null)
            {
                allPartArgs.Clear();
                allPartArgs = null;
            }

            curParts = null;
            defaultArgs = null;
        }
    }
}